var fn = require('./fn.js');
var rs;
var thisur;
var thisurl;
var thisbaseurl;
var result;
var header = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': ""
};

function fxurls(url){
    urls=getdd(url);
    return urls;
}

exports.create= function (url, comeback)
{
    exports.thisurl=thisurl=url;
    var async = require('async');
    async.series({
            getRs: function (cb) {
                exports.host = fn.gethost(url);
                exports.host2 = fn.gethost2(url);
                nextpagerem = "";
                rssql = require("./zqnewsmysql");
                rssql.getrs(exports.host, function (result) {
                    result = (result == undefined) ? {} : result;
                    rs = exports.rs = result;
                    nextpage = "";
                    bianma=(rs.gbk==1)?"gbk":"utf8";
                    cb();
                },exports.host2);
            },
            getHTML: function (cb) {
                var fs = require('fs');
                file = "d:/temp/" + fn.substr(fn.preg_replace("\\:|\\/|\\?|\\=|\\&|\\-|\\%|\\;|\\-", "", url).replace(/-/g, ""), 0, 100);
                fs.readFile(file, 'utf-8', function (err, data) {
                    if (err) {
                        filename = "d:\\temp\\" + url.replace(/\/|\:|\?|\=|\&|\-|%|;|-/gi, "").replace(/\//, "\\").substring(0, 100);
                        filename = filename.trim();
                        if (!fs.existsSync(filename)) {
                            var ng = require('nodegrass');
                            ng.get(url, function (data, status, headers) {
                                if(rs.gbk==null||rs.gbk==undefined||rs.gbk==""){
                                    var isgbk=fn.isgbk(data);
                                    isgbk=(isgbk=="gb2312"||isgbk=="gbk")?"gbk":"utf8";
                                    rssql.setgbk(host2,isgbk);
                                }
                                filename = "d:\\temp\\" + url.replace(/\/|\:|\?|\=|\&|\-|%|;|-/gi, "").replace(/\//, "\\").substring(0, 200);
                                filename = filename.trim();
                                re=/�/
                                if(!re.test(data)) {
                                    var fs = require('fs');
                                    fs.writeFile(filename, data, function (err) {
                                        if (err) throw err;
                                        console.log("д��" + url);
                                        exports.html = data;
                                        cb();
                                    });
                                }
                            }, header,bianma).on('error', function (e) {
                                console.log("error:" + url + " " + e.message);
                            });
                        }
                    } else {
                        exports.html = data;
                        cb();
                    }
                });
            },geturls: function (cb) {
                exports.result=dogeturls();
                comeback(exports.result);
            }
        },
        function (err, results2) {
            console.log("byebye");
        });
}

function getbaseurl(html){
    nn = new Array();
    thisbaseurl = "";
    if (fn.preg_match("/<base\\s+href=\"([^<>\\s]+)\"/isx", html, nn)) {
        if (nn.length > 0) {
            baseurl = nn[1];
            thisbaseurl = baseurl;
        }
    }
    return thisbaseurl;
}

function dogeturls() {
    var textarea="";
    if (rs.hrefbtre != "null"|| rs.hrefbtre != "") {
        cc = fn.preg_split("/\|\|\||$/m", rs.hrefbtre);

        for (i = 0; i < cc.length; i++) {
            if (fn.trim(cc[i]) == "") continue;
            if (!fn.preg_match("/\&\&\&/Uisx", cc[i])) {
                mm4 = new Array();
                if (preg_match("/\:nn([0-9]+)(?=$|\:)/Uisx", fn.trim(cc[i]), mm4)) {
                    cc[i] = fn.preg_replace("/\:nn[0-9]+(?=$|\:)/Uisx", "", fn.trim(cc[i]));
                    nnmm41 = new Array();
                    fn.preg_match_all("/" + fn.trim(cc[i]) + "/Uisx", exports.html, nnmm41);
                    eval("nn" + mm4[1] + "=nnmm41;");
                } else {
                    if (fn.trim(textarea) == "") {
                        cc[i] = fn.trim(cc[i]);
                        nn = new Array();
                        fn.preg_match_all("/" + fn.trim(cc[i]) + "/Uisx", exports.html, nn);
                        textarea = fn.trim(nn[1][0]);
                    }
                }
            } else {
                ddd = fn.preg_split("&&&", cc[i]);
                for (d = 0; d < ddd.length - 1; d++) {
                    nn = new Array();
                    fn.preg_match_all("/" + fn.trim(ddd[d]) + "/Uisx", exports.html, nn);
                    if (fn.trim(nn[1][0]) == "") {
                        break;
                    } else {
                        tempnr = nn[1][0];
                    }
                }

                if (fn.preg_match("/\:nn([0-9]+)(?=\:|$)/Uisx", fn.trim(ddd[ddd.length - 1]), mm3)) {
                    ddd[ddd.length - 1] = fn.preg_replace("/\:nn[0-9]+(?=\:|$)/Uisx", "", fn.trim(ddd[ddd.length - 1]));
                    nnmm31 = new Array();
                    fn.preg_match_all("/" + fn.trim(ddd[ddd.length - 1]) + "/Uisx", exports.html, nnmm31);
                    eval("nn" + mm3[1] + "=nnmm31;");
                    if (fn.trim(eval("nn" + mm3[1] + "[1][0]")) == "") break;
                    textarea = tempnr = eval("nn" + mm3[1] + "[1][0]");
                    //$params=array("nn22"=>$nn22,"nn11"=>$nn11,"nn99"=>$nn99);
                    //$textarea=evalpublic($tempnr,$this->url,$params,$rs,$this->bug);
                } else {
                    textarea = eval("nn" + mm3[1] + "[1][0]");
                }
            }
            if (fn.trim(textarea)) {
                break;
            }
        }
    }

//if($textarea!=""&&count($nn22['href'])==0){
//    preg_match_all("/<a[^<>]*href=\"(?<href>[^<>\s\"\']*)\"[^<>]*>(?<title>.*)<\/a>/Uisx",$textarea,$nn22);
//}
//
//$params=array("nn22"=>$nn22,"nn11"=>$nn11,"nn99"=>$nn99);
//$textarea=evalpublic($textarea,$this->url,$params,$rs,$thisbaseurl,$thissecexe,1);


if(fn.trim(textarea)==""){
    var thistemp = exports.html.replace(/<(script|embed|object|style)[^<>]*>[\s\S]*?<\/\1>/gi,"").replace(/<(?:(\/?)(?!img|br|b|strong|a))([a-z0-9]+)[^<>]*>/gi,"<$1$2>").replace(/<(?!(\/?)(?:a|img|b|p|br|strong|td|div))[^<>]*>/gi,"");
    thistemp = fn.preg_replace("/<?!(img|a|b|p|br|font|em|strong|embed|object)\\s*[^<>]*>[^<>]*<\\/\\1>/isx", "", fn.strip_tags(thistemp,"<img><div><td><br><p><a>"));
    thistemp = fn.preg_replace("/<(script|style)[^<>]*>.*<\/\\1>/Uisx", "", thistemp);
    thistemp = fn.preg_replace("/<div[^<>]*>(?:\\s*<div[^<>]*>)?(<\\/?(a|span|p|br|embed|object)[^<>]*>|\\s|\\&nbsp\\;)*(<img[^<>]*>)(<" + "?\\/?(a|span|p|br|embed|object)[^<>]*>|([^\\<\\>a-z0-9_\\s]|\\&nbsp\\;|\\s))*<\\/div>(\\s*<\\/div>)??/Uisx", "\\3\\4", thistemp);
    thistemp = thistemp.replace(/<div[^<>]*>((?:[\s\S](?!<div))*?)(��ҳ|��һҳ)((?:[\s\S](?!<div))*?)<\/div>/gi,"");
    nn = new Array();
    nn[3] = new Array();
    n = 0;
    result=new Array();
    do{
        nn=new Array();nn[0]=new Array();
        fn.preg_match_all("/<div[^<>]*>((?:[\\s\\S](?!<div))*)<\\/div>/Uisx", thistemp, nn);
        for (i = 0; i < nn[0].length; i++) {
            ss=new Array();ss[0]=new Array();
            fn.preg_match_all("/<a\\s[^<>]*href=([^\\s\\>]*?)/Uisx",nn[0][i],ss);
            if(ss[0].length>=6){
                result.push(ss[1]);
                thistemp=fn.str_replace(nn[0][i],"",thistemp);
            }else{
                thistemp=fn.str_replace(nn[0][i],"",thistemp);
            }
        }
        n++;
    }while(nn[0].length>0&&n<5&&nn[0].length>0);
    //console.log(result);
}else{
    //$this->resulty[href]=nn22[href];
    //$this->resulty[title]=nn22[title];
    result[0]=nn22['href'];
}
    result=quanlj(result);
return result;
}

function quanlj(array){
    for(i=0;i<array.length;i++){
        arr=array[i];
        for(k=0;k<arr.length;k++){
            v=arr[k];
            v=fn.preg_replace("/^(\\s|'|\")+|(\\s|'|\")+$/","",v);
            v=fn.quanurl(v,thisurl,thisbaseurl).replace(/\"|'/,"");
            array[i][k]=v;
        }
        if(typeof arr[i]=="object") arr[i]=rekey(arr[i]);
    }
    return array;
}

exports.themax=function(){
    max=new Array();
    temp=new Array();
    if(exports.result==undefined) return [];
    for(k=0;k<exports.result.length;k++){
            v=exports.result[k];
            if(v.length>temp.length){
                max=v;
                temp.push(v);
            }
    }
    return max;
}

function rekey(arr){
    newarr=new Array();
    for(v in arr){
        newarr.push(v);
    }
    return newarr;
}

function theindex(index){
    index=(index==undefined||index=="")?"max":index;
    if(index=="max"){
        return themax();
    }
    if(index==""){
        index=0;
    }
    max=exports.result;
    return max[index];
}

